//
//  YYDiscoverSearchView.swift
//  YYWeibo
//
//  Created by strongwallyi on 15/12/26.
//  Copyright © 2015年 strongwallyi. All rights reserved.
//

import UIKit

class YYDiscoverSearchView: UIView {
    
    //从 xib 中加载view
    class func search() -> YYDiscoverSearchView{
        return NSBundle.mainBundle().loadNibNamed("YYDiscoverSearch", owner: nil, options: nil).last as! YYDiscoverSearchView
    }
    
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var cancleBtn: UIButton!
    @IBOutlet weak var textFiledConstraint: NSLayoutConstraint!
    
    //搜索框内的放大镜
    override func awakeFromNib() {
        let leftView = UIImageView()
        leftView.image = UIImage(named: "searchbar_textfield_search_icon")
        leftView.frame.size = CGSizeMake(frame.height, frame.height)
        textField.leftView = leftView
        //center 代表在图片以鸳鸯显示
        leftView.contentMode = .Center
        textField.leftViewMode = .Always
        
    }
    
    
    @IBAction func textFiledEditingBegin(sender: AnyObject) {
        print("发现,开始输入")
        textFiledConstraint.constant = cancleBtn.bounds.size.width + 10
        //设置动画
        UIView.animateWithDuration(0.25) { () -> Void in
            self.textField.layoutIfNeeded()
        }
    }
    
    @IBAction func cancleBtnClick() {
        //回退键盘
        self.textField.resignFirstResponder()
        
        //让搜索框的宽度回到原来的位置
        textFiledConstraint.constant = 0
        //设置动画
        UIView.animateWithDuration(0.25) { () -> Void in
            self.textField.layoutIfNeeded()
        }
    }
    
    

    
}
